package com.chanjet.donkey.web.attach;

import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.chanjet.donkey.entity.Attach;
import com.chanjet.donkey.repository.AttachDao;
import com.chanjet.donkey.util.Constants;


@Controller
@RequestMapping(value = "/attach")
public class AttachController {
	@Autowired
	private AttachDao attachDao;
	
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public void list(@PathVariable Long id, HttpServletResponse res) throws IOException {
		Attach attach = attachDao.findOne(id);
		res.setContentType(attach.getType());
		res.setContentLength((int) attach.getSize());
		FileInputStream fis = new FileInputStream(Constants.UPLOAD_PATH + attach.getSaveName());
		IOUtils.copy(fis, res.getOutputStream());
	}
}
